package com.dbtools;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.avecsys.quotemanager.constants.BusinessCodes;
import com.avecsys.quotemanager.dao.common.BaseDAO;
import com.avecsys.quotemanager.dao.common.EnumDBRecordStatus;
import com.avecsys.quotemanager.pojos.CompanyMasterPojo;
import com.avecsys.quotemanager.pojos.CountryMasterPojo;
import com.avecsys.quotemanager.pojos.SystemUsersPojo;
import com.avecsys.quotemanager.pojos.UserMasterPojo;

public class InitialUserSetup
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(
				"applicationContext.xml");
		createInitialUser(applicationContext);
	}

	private static void createInitialUser(ApplicationContext applicationContext)
	{
		BaseDAO baseDAO = (BaseDAO) applicationContext.getBean("baseDAO");
		Session session = baseDAO.getHibernateTemplate().getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		CompanyMasterPojo companyMasterPojo = new CompanyMasterPojo();
		companyMasterPojo.setCompanyName("QuoteManagerGroup");
		companyMasterPojo.setCompanyId(new Long("1"));
		companyMasterPojo.setAddressLine1("Singapore Add1");
		companyMasterPojo.setAddressLine2("Singapore Add2");
		companyMasterPojo.setCity("Singapore");
		companyMasterPojo.setVersion(new Long("0"));
		companyMasterPojo.setStatus(EnumDBRecordStatus.ACTIVE.status());
		companyMasterPojo.setCompanyType(BusinessCodes.COMPANY_TYPE_SYSTEM);
		CountryMasterPojo countryMasterPojo = (CountryMasterPojo) session.load(
				CountryMasterPojo.class,
					"SG");
		companyMasterPojo.setCountryMasterPojo(countryMasterPojo);
		session.save(companyMasterPojo);

		UserMasterPojo userMasterPojo = new UserMasterPojo();
		userMasterPojo.setEmail("admin@qm.com");
		userMasterPojo.setUserName("SPG");
		userMasterPojo.setUserId(new Long("1"));
		userMasterPojo.setVersion(new Long("0"));
		userMasterPojo.setStatus(EnumDBRecordStatus.ACTIVE.status());
		userMasterPojo.setPassword("W6ph5Mm5Pz8GgiULbPgzG37m");
		userMasterPojo.setEmailVerfied(Boolean.TRUE.toString());
		userMasterPojo.setUserType(BusinessCodes.USER_TYPE_SYSTEM_ADMIN);
		session.save(userMasterPojo);

		SystemUsersPojo systemUsersPojo = new SystemUsersPojo();
		systemUsersPojo.setSystemUserId(new Long("1"));
		systemUsersPojo.setCompanyMasterPojo(companyMasterPojo);
		systemUsersPojo.setUserMasterPojo(userMasterPojo);
		systemUsersPojo.setVersion(new Long("0"));
		systemUsersPojo.setIsAdmin(Boolean.TRUE.toString());
		systemUsersPojo.setStatus(EnumDBRecordStatus.ACTIVE.status());
		session.save(systemUsersPojo);
		tx.commit();
	}

}
